package com.lemon.reggie.controller;


import com.lemon.reggie.common.R;
import com.lemon.reggie.pojo.dto.DefaultAddressBookDTO;
import com.lemon.reggie.pojo.dto.EditAddressBookDTO;
import com.lemon.reggie.pojo.dto.SaveAddressBookDTO;
import com.lemon.reggie.pojo.vo.AddressBookVO;
import com.lemon.reggie.service.AddressBookService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 地址管理 前端控制器
 * </p>
 *
 * @author Lemon
 * @since 2022-05-23
 */
@RestController
@RequestMapping("/addressBook")
@Validated
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;

    @PostMapping
    public R<?> saveAddressBook(@RequestBody @Valid SaveAddressBookDTO saveAddressBookDTO) {
        addressBookService.saveAddressBook(saveAddressBookDTO);
        return R.success("添加成功!");
    }

    @GetMapping("/list")
    public R<?> listAddressBook() {
        List<AddressBookVO> addressBookVOList = addressBookService.listAddressBook();
        return R.success(addressBookVOList);
    }

    @GetMapping("/{id}")
    public R<?> getAddressBookVO(@PathVariable("id") Long id) {
        AddressBookVO addressBookVO = addressBookService.getAddressBookVO(id);
        return R.success(addressBookVO);
    }

    @PutMapping("/default")
    public R<?> defaultAddressBook(@RequestBody @Valid DefaultAddressBookDTO defaultAddressBookDTO) {
        AddressBookVO addressBookVO = addressBookService.defaultAddressBook(defaultAddressBookDTO);
        return R.success(addressBookVO);
    }

    @PutMapping
    public R<?> editAddressBook(@RequestBody @Valid EditAddressBookDTO editAddressBookDTO) {
        addressBookService.editAddressBook(editAddressBookDTO);
        return R.success("修改成功！");
    }

    @DeleteMapping
    public R<?> deleteAddressBook(@RequestParam("ids") List<Long> ids) {
        addressBookService.deleteAddressBook(ids);
        return R.success("删除成功！");
    }

    @GetMapping("/default")
    public R<?> getUserDefault() {
        AddressBookVO addressBookVO = addressBookService.getUserDefault();
        return R.success(addressBookVO);
    }
}

